package com.liyueheng.app.dataWarehouseDetail.behavior

import com.liyueheng.util.{ConfigLoader, SparkConf}
import org.apache.spark.sql.functions.{col, count, when}

object PageViews {
  def pageViews(): Unit = {

println("------------------ 分析页面浏览量 -----------------")
    val databaseDwd = ConfigLoader.getString("databases.dwd") // 获取 dwd 数据库名
    val appName = ConfigLoader.getString("app.name") // 获取应用名称
    val databaseDws = ConfigLoader.getString("databases.dws") // 获取 dws 数据库名
    val spark = SparkConf.createSparkSession(appName)

    val userActDF = spark.table(s"$databaseDwd.user_act")

    val pvDF = userActDF
      .groupBy("act_date")
      .agg(
        count(when(col("act_type") === 1, col("user"))).alias("value")
      )
      .orderBy("act_date")
      .withColumnRenamed("act_date", "name")

    // 保存为 PV 表
    pvDF.write.mode("overwrite").format("hive").saveAsTable(s"$databaseDws.detail_pv")
  }

}
